package com.zang.hash;

import java.util.HashSet;

/**
 * @author ZVerify
 * @since 2022/10/13 09:52
 **/
public class 快乐数 {
    public boolean isHappy(int n) {

        HashSet<Integer> hashSet = new HashSet<>();

        while (n!=1 && !hashSet.contains(n)){

            hashSet.add(n);
            n = pingfang(n);
        }
        return n == 1;
    }

    private int pingfang(int n) {

        int sum = 0;
        while (n>0){
            int i =  n%10;
            sum += i*i;
            n = n/10;
        }

        return sum;
    }
}
